<script setup>
  import { http } from '@/utils/http.js'
  import {
    mobAndPasList,
    isAgreement,
    mobAndPassRules,
  } from './mobAndPassData.js'

  import { ref } from 'vue'
  import { useloginStore } from '@/store/user.js'

  //失焦校验
  const passwordform = ref(null)
  function mobilecheckfn() {
    passwordform.value.validateField('mobile')
  }
  function passwordcheckfn() {
    passwordform.value.validateField('password')
  }

  // 登录
  async function passwordSunmitfn() {
    if (!isAgreement.value) return uni.utils.toast('请同意协议！')
    try {
      await passwordform.value.validate()
      const { data } = await http.post('/login/password', {
        password: mobAndPasList.value.password,
        mobile: mobAndPasList.value.mobile,
      })

      console.log(data.data)

      useloginStore().token = data.data.token
      useloginStore().refreshToken = data.data.refreshToken
      useloginStore().userId = data.data.id

      const reductUrl = useloginStore().redictUrl
      const openType = useloginStore().openType

      uni[openType]({
        url: '/' + reductUrl,
      })
    } catch (e) {
      //TODO handle the exception
      console.log(e)
    }
  }
</script>
<template>
  <uni-forms
    class="login-form"
    ref="passwordform"
    v-bind:modelValue="mobAndPasList"
    v-bind:rules="mobAndPassRules"
  >
    <uni-forms-item name="mobile" class="mobileinp">
      <uni-easyinput
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        v-model="mobAndPasList.mobile"
        placeholder-style="color: #C3C3C5"
        @blur="mobilecheckfn"
      />
    </uni-forms-item>

    <uni-forms-item name="password">
      <uni-easyinput
        type="password"
        placeholder="请输入密码"
        :input-border="false"
        v-model="mobAndPasList.password"
        @blur="passwordcheckfn"
        placeholder-style="color: #C3C3C5"
      />
    </uni-forms-item>
    <view class="agreement">
      <radio
        :checked="isAgreement"
        v-on:click="isAgreement = !isAgreement"
        color="#16C2A3"
      />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @click="passwordSunmitfn">登 录</button>
    <navigator hover-class="none" class="uni-navigator" url=" ">
      忘记密码？
    </navigator>
  </uni-forms>
</template>

<script>
  export default {
    options: {
      styleIsolation: 'shared',
    },
  }
</script>

<style lang="scss">
  @import './styles.scss';
</style>
